'''
Created on Nov 20, 2013

@author: devuser
'''

from flask import Flask
from flask import url_for
from flask import send_from_directory



##
##CODE TO INITIALIZE THE DATABASE CONNECTION BEGINS HERE
##

import MySQLdb
import ConfigParser
import sys

# First, read in the config file for database connection parameters and exit on failure
try:
    db_config = ConfigParser.ConfigParser()
    db_config.read("../database.ini")
except:
    sys.exit("Failed to read configuration file")
    

# ConfigMapper creates a dictionary of the options from a given section of the config file
# If any option generates an exception, then set that option to an empty string
def ConfigMapper(config_section):
    conf_dict = {}
    options = db_config.options(config_section)
    for opt in options:
            try:
                    conf_dict[opt] = db_config.get(config_section, opt)
            except:
                    print("Exception on %s!" % opt)
                    conf_dict[opt] = ""
    return conf_dict

# Get a dictionary of the Database section of the config file (currently the only section)
db_options = ConfigMapper("Database")

# Try connecting to the database with the given information

try:
    db_connection = MySQLdb.connect(
                                host= db_options["host"],
                                port= int(db_options["port"]),  # @UndefinedVariable
                                user= db_options["user"],
                                passwd= db_options["pass"],
                                db= db_options["schema"])
except:
    sys.exit("Failed to connect to database")
    #print sys.exc_info()

##
##CODE TO INITIALIZE THE DATABASE CONNECTION ENDS HERE
##

app = Flask(__name__)  # @UndefinedVariable

@app.route('/')
def hello_world():
    db_cursor = db_connection.cursor()
    db_cursor.callproc("hello_world", () )
    results = db_cursor.fetchone()
    db_cursor.close()
    
    return results

if __name__ == '__main__':  # @UndefinedVariable
    app.run()
    

@app.route('/upload')
def upload():
    url_for('static', filename='upload') 
    return send_from_directory('static', '/article.html')
    print "url_for"

if __name__ == "__main__":
    app.run(debug=True)
    